﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Threading;
using DataRecv.enums;
using System.Runtime.InteropServices;
using DataRecv.tools;
using DataRecv.models.sdk.DAHUASDK;
using JRCore.protocol;
using System.IO;
using DataRecv.models.recever;
using System.Drawing;
using System.Drawing.Imaging;

namespace DataRecv.models.device
{
    public class DH:Device
    {
        public new static Enum_CaptureSystem Ability =
            Enum_CaptureSystem.卡口 |
            Enum_CaptureSystem.逆行 |
            Enum_CaptureSystem.超速 |
            Enum_CaptureSystem.闯红灯 |
            Enum_CaptureSystem.压线 |
            Enum_CaptureSystem.违章停车 |
            Enum_CaptureSystem.黄牌禁限 |
            Enum_CaptureSystem.不按导向行驶;

        public static char[] spliter = new char[2] { '\0', ' ' };
        private NET_DEVICEINFO m_deviceInfo;
        private fAnalyzerDataCallBack m_DHAnalyzerDataCallBack;
        private DataRecvRecord m_record;
        private byte m_index;
        private byte m_count;
        public DH(DeviceView view)
        {
            try
            {
                deviceview = view;
                state = new DeviceState();
                state.Port = 37777;
                state.Ping = false;
                state.LoginID = -1;
                state.Channel = 0;
                state.IsOpen = false;
                state.PlayHandle = -2;
                state.AlarmHandle = -1;
                
                m_deviceInfo = new NET_DEVICEINFO();
                m_record = new DataRecvRecord();
            }
            catch { }
        }

        private void ResetRecord()
        {
            try
            {
                if (m_record != null)
                {
                    for (int i = 0; i < m_record.P.Length; i++)
                    {
                        m_record.P[i] = "";
                    }
                    m_record.Pic1 = null;
                    m_record.Pic2 = null;
                    m_record.Pic3 = null;
                    m_record.Pic4 = null;
                    m_record.Pic5 = null;
                    m_record.Pic6 = null;
                    m_record.Pic7 = null;
                }
            }
            catch { }
        }

        public override void Init(object obj)
        {
            try
            {
                //state.Ping = G.PingIP(deviceview.deviceparam.ip);
                //大华SDK能够检测到连接建立或断开不用ping
                if (PingReport != null) PingReport.Invoke(deviceview.deviceparam.ip, state.Ping);

                if (state.Ping == false)
                {
                    LogTool.log.Info("DH摄像机---" + deviceview.deviceparam.ip + "---Ping不通");
                }

                int error = 0;
                if (state.LoginID <= 0)
                {
                    state.LoginID = DHClient.DHLogin(deviceview.deviceparam.ip, (ushort)state.Port, deviceview.deviceparam.deviceusername, deviceview.deviceparam.devicepassword, out m_deviceInfo, out error);
                    if (state.LoginID <= 0)
                    {
                        LogTool.log.Info("DH摄像机---" + deviceview.deviceparam.ip + "---注册失败");
                        return;
                    }
                }

                if (state.AlarmHandle <= 0)
                {
                    m_DHAnalyzerDataCallBack = new fAnalyzerDataCallBack(DHCallBackFunc);
                    state.AlarmHandle = DHClient.DHRealLoadPicture(state.LoginID, state.Channel, EventIvs.EVENT_IVS_ALL, m_DHAnalyzerDataCallBack, 0);
                    if (state.AlarmHandle <= 0)
                    {
                        LogTool.log.Info("DH摄像机---" + deviceview.deviceparam.ip + "---回调函数注册失败");
                    }
                    else
                    {
                        state.Ping = true;
                        if (PingReport != null) PingReport.Invoke(deviceview.deviceparam.ip, state.Ping);
                        LogTool.log.Info("DH摄像机---" + deviceview.deviceparam.ip + "---注册布防成功");
                        OpenAlarmTrigger();
                    }
                }
            }
            catch (Exception er)
            {
                LogTool.log.Info("DH.Init()---" + deviceview.deviceparam.ip + "---" + er.Message);
            }
        }

        /// <summary>
        /// 打开布防开关
        /// </summary>
        /// <returns></returns>
        private bool OpenAlarmTrigger()
        {
            bool ans = false;
            ALARMCTRL_PARAM ioParam = new ALARMCTRL_PARAM();
            ioParam.dwSize = (uint)Marshal.SizeOf(ioParam);
            ioParam.nAlarmNo = 0; //报警输出通道编号
            ioParam.nAction = 1; // 1：触发报警；0：停止报警

            IntPtr pAlarmCtrl = Marshal.AllocHGlobal(Marshal.SizeOf(ioParam));
            try
            {
                Marshal.StructureToPtr(ioParam, pAlarmCtrl, true);
                ans = DHClient.DHControlDevice(state.LoginID, CtrlType.DH_TRIGGER_ALARM_OUT, pAlarmCtrl, 3000);
            }
            catch (Exception er)
            {
                LogTool.log.Info("DH.OpenAlarmTrigger()---" + deviceview.deviceparam.ip + "---" + er.Message);
            }
            finally
            {
                Marshal.FreeHGlobal(pAlarmCtrl);
            }
            return ans;
        }

        private int DHCallBackFunc(int lAnalyzerHandle, uint dwAlarmType, IntPtr pAlarmInfo, IntPtr pBuffer, uint dwBufSize, uint dwUser, int nSequence, IntPtr reserved)
        {
            try
            {
                Recever_Bag bag = new Recever_Bag();
                G.N++;
                bag.ip = deviceview.deviceparam.ip;
                bag.type = "DH";
                bag.id = G.N;
                bag.bz = dwAlarmType.ToString();
                if (LogTool.gDebugEnable) LogTool.log.Info("1 " + bag.Txt());
                bag.dwAlarmType = dwAlarmType;
                if (dwBufSize == 0)
                {
                    if (LogTool.gDebugEnable) LogTool.log.Info("1 DH N=" + bag.id.ToString() + " dwBufSize==0");
                    return 1;
                }

                try
                {
                    bag.pBuffer = new byte[(int)dwBufSize];
                    Marshal.Copy(pBuffer, bag.pBuffer, 0, (int)dwBufSize);
                }
                catch(Exception er) 
                {
                    if (LogTool.gDebugEnable) LogTool.log.Info("1 DH N=" + bag.id.ToString() + " Marshal.Copy()" + er.Message);
                    return 1;
                }

                try
                {
                    if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFICGATE)//交通卡口事件
                    {
                        DEV_EVENT_TRAFFICGATE_INFO Info = new DEV_EVENT_TRAFFICGATE_INFO();
                        Info = (DEV_EVENT_TRAFFICGATE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFICGATE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        ExTractPlate((bag.platepic==null), bag.pBuffer,deviceview.deviceparam.picwidth,deviceview.deviceparam.picheight, Info.stuObject, 
                            ref bag.platepic, ref bag.top, ref bag.left, ref bag.bottom, ref bag.right);
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFICJUNCTION)//交通路口事件
                    {
                        DEV_EVENT_TRAFFICJUNCTION_INFO Info = new DEV_EVENT_TRAFFICJUNCTION_INFO();
                        Info = (DEV_EVENT_TRAFFICJUNCTION_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFICJUNCTION_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        ExTractPlate((bag.platepic == null), bag.pBuffer, deviceview.deviceparam.picwidth, deviceview.deviceparam.picheight, Info.stuObject, 
                             ref bag.platepic, ref bag.top, ref bag.left, ref bag.bottom, ref bag.right);
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_RUNREDLIGHT)//交通违章-闯红灯事件
                    {
                        DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO Info = new DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO();
                        Info = (DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_TURNLEFT)//交通违章-违章左转
                    {
                        DEV_EVENT_TRAFFIC_TURNLEFT_INFO Info = new DEV_EVENT_TRAFFIC_TURNLEFT_INFO();
                        Info = (DEV_EVENT_TRAFFIC_TURNLEFT_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_TURNLEFT_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_TURNRIGHT)//交通违章-违章右转
                    {
                        DEV_EVENT_TRAFFIC_TURNRIGHT_INFO Info = new DEV_EVENT_TRAFFIC_TURNRIGHT_INFO();
                        Info = (DEV_EVENT_TRAFFIC_TURNRIGHT_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_TURNRIGHT_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERSPEED)//交通违章-超速
                    {
                        DEV_EVENT_TRAFFIC_OVERSPEED_INFO Info = new DEV_EVENT_TRAFFIC_OVERSPEED_INFO();
                        Info = (DEV_EVENT_TRAFFIC_OVERSPEED_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_OVERSPEED_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        ExTractPlate((bag.platepic == null), bag.pBuffer, deviceview.deviceparam.picwidth, deviceview.deviceparam.picheight, Info.stuObject, 
                            ref bag.platepic, ref bag.top, ref bag.left, ref bag.bottom, ref bag.right);
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_UNDERSPEED)//交通违章-低速
                    {
                        DEV_EVENT_TRAFFIC_UNDERSPEED_INFO Info = new DEV_EVENT_TRAFFIC_UNDERSPEED_INFO();
                        Info = (DEV_EVENT_TRAFFIC_UNDERSPEED_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_UNDERSPEED_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_MANUALSNAP)//交通手动抓拍事件
                    {
                        DEV_EVENT_TRAFFIC_MANUALSNAP_INFO Info = new DEV_EVENT_TRAFFIC_MANUALSNAP_INFO();
                        Info = (DEV_EVENT_TRAFFIC_MANUALSNAP_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_MANUALSNAP_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKING)//交通违章停车
                    {
                        DEV_EVENT_TRAFFIC_PARKING_INFO Info = new DEV_EVENT_TRAFFIC_PARKING_INFO();
                        Info = (DEV_EVENT_TRAFFIC_PARKING_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_PARKING_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKINGSPACENOPARKING)//车位无车事件
                    {
                        DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO Info = new DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO();
                        Info = (DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKINGSPACEPARKING)//车位有车事件
                    {
                        DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO Info = new DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO();
                        Info = (DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }

                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_WRONGROUTE)//不按车道行驶
                    {
                        DEV_EVENT_TRAFFIC_WRONGROUTE_INFO Info = new DEV_EVENT_TRAFFIC_WRONGROUTE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_WRONGROUTE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_WRONGROUTE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;

                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_RETROGRADE)//逆行
                    {
                        DEV_EVENT_TRAFFIC_RETROGRADE_INFO Info = new DEV_EVENT_TRAFFIC_RETROGRADE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_RETROGRADE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_RETROGRADE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERYELLOWLINE)//压黄线
                    {
                        DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO Info = new DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERLINE)//压车道线（压白线）
                    {
                        DEV_EVENT_TRAFFIC_OVERLINE_INFO Info = new DEV_EVENT_TRAFFIC_OVERLINE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_OVERLINE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_OVERLINE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_CROSSLANE)//违章变道
                    {
                        DEV_EVENT_TRAFFIC_CROSSLANE_INFO Info = new DEV_EVENT_TRAFFIC_CROSSLANE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_CROSSLANE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_CROSSLANE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_YELLOWPLATEINLANE)//黄牌占道，闯禁区
                    {
                        //EVENT_IVS_TRAFFIC_YELLOWPLATEINLANE
                        DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO Info = new DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO();
                        Info = (DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO)Marshal.PtrToStructure(pAlarmInfo, typeof(DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO));
                        if ((Info.stuObject.bPicEnble == 1) && (Info.stuObject.stPicInfo.dwFileLenth > 0))
                        {
                            bag.platepic = new byte[Info.stuObject.stPicInfo.dwFileLenth];
                            IntPtr pSmapic = (IntPtr)(pBuffer.ToInt32() + (int)(Info.stuObject.stPicInfo.dwOffSet));
                            Marshal.Copy(pSmapic, bag.platepic, 0, bag.platepic.Length);
                        }
 
                        bag.dhinfo = Info;
                    }
                    else if (dwAlarmType == EventIvs.EVENT_ALARM_MOTIONDETECT)//视频移动侦测事件
                    {
                        return 1;//不用处理此种事件（睢宁智能球机违停抓拍）
                    }
                    else
                    {
                        LogTool.log.Info("未处理事件dwAlarmType = " + dwAlarmType.ToString());
                        return 1;
                    }

                    ThreadPool.QueueUserWorkItem(new WaitCallback(DHThreadFunc), bag);
                    return 1;
                }
                catch(Exception er) 
                {
                    if (LogTool.gDebugEnable) LogTool.log.Info("1 DH N=" + bag.id.ToString() + " Marshal.PtrToStructure()" + er.Message);
                }
                return 1;
            }
            catch (Exception er)
            {
                GC.Collect();
                LogTool.log.Info("DHCallBackFunc()" + er.Message);
            }
            return 0;
        }

        public static void ExTractPlate(bool koutu,byte[] imgbuffer,int nWidth,int nHeight, DH_MSG_OBJECT plateObj, ref byte[] platebytes, ref int top, ref int left,ref int bottom,ref int right)
        {
            MemoryStream ms1 = null;
            MemoryStream ms2 = null;
            top = 0;
            left = 0;
            right = 0;
            bottom = 0;
            Bitmap bmp = null;
            try
            {
                if ((imgbuffer != null) &&
                    (plateObj.BoundingBox.top > 0) && (plateObj.BoundingBox.bottom > 0)&&
                    (nWidth>0)&&(nHeight>0))
                {
                   

                    //1.BoundingBox的值是在8192*8192坐标系下的值，必须转化为图片中的坐标
                    //2.OSD在图片中占了64行,如果没有OSD，下面的关于OSD的处理需要去掉(把OSD_HEIGHT置为0)
                    const int OSD_HEIGHT = 64;
                    //const int HIMETRIC_INCH = 2540;

                    int noOSDHeight = nHeight - OSD_HEIGHT;

                    left = (int)Math.Ceiling(((nWidth * plateObj.BoundingBox.left)) / 8192.0);
                    right = (int)Math.Ceiling((nWidth * plateObj.BoundingBox.right) / 8192.0);
                    bottom = (int)Math.Ceiling((noOSDHeight * plateObj.BoundingBox.bottom) / 8192.0) + OSD_HEIGHT;
                    top = (int)Math.Ceiling((noOSDHeight * plateObj.BoundingBox.top) / 8192.0) + OSD_HEIGHT;

                    if (koutu == true)
                    {
                        ms1 = new MemoryStream(imgbuffer);
                        bmp = new Bitmap(ms1);

                        Rectangle rect = new Rectangle(left, top, right - left, bottom - top);

                        ms2 = new MemoryStream();
                        bmp.Clone(rect, bmp.PixelFormat).Save(ms2, ImageFormat.Jpeg);
                        platebytes = ms2.ToArray();
                    }
                }
            }
            catch { }
            finally
            {
                if (ms1 != null) ms1.Dispose();
                if (ms2 != null) ms2.Dispose();
                if (bmp != null) bmp.Dispose();
            }
        }

        public void DHThreadFunc(object obj)
        {
            if (obj == null) return;
            Recever_Bag bag = (Recever_Bag)obj;
            if (LogTool.gDebugEnable) LogTool.log.Info("2 " + bag.Txt());
            uint dwAlarmType = bag.dwAlarmType;
            try
            {
                #region 1数据转换

                string P1 = "";
                string P2 = "";
                string P3 = "";
                string P4 = "";
                string P5 = "";
                string P6 = "";
                string P7 = "";
                string P8 = "";
                string P9 = "";
                string P10 = "";
                string P11 = "";
                string P12 = "";
                string P13 = "";
                string P14 = "";
                string P15 = "";
                string P16 = "";
                string P17 = "";
                string P18 = "";
                string P19 = "";
                string P20 = "";
                string P21 = "";
                string P22 = "";
                string P23 = "";
                string P24 = "";
                string P25 = "";
                string P26 = "";
                string P27 = "";
                string P28 = "";
                string P29 = "";
                string P44 = "";
                string P45 = "Unknown";
                string P46 = "0,0,0,0";
                Enum_CaptureSystem cs = Enum_CaptureSystem.NONE;//确定记录类型


                if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFICGATE)//交通卡口事件
                {
                    #region 卡口
                    DEV_EVENT_TRAFFICGATE_INFO Result = (DEV_EVENT_TRAFFICGATE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.卡口;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "0";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "2";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    if (Result.stuVehicle.bColor == 1)
                    {
                        P24 = GetColorCode(Result.stuVehicle.rgbaMainColor);//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    }
                    else
                    {
                        P24 = "";
                    }
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    P44 = Encoding.Default.GetString(Result.szVehicleType).Trim('\0');
                    P45 = TrimStrangeChar(Encoding.Default.GetString(Result.stuVehicle.szText).Trim(spliter));
                    P46 = bag.PositionString;
                    //车辆大小类型，"Motor" 摩托车车 "Light-duty" 小型车 "Medium" 中型车 "Oversize" 大型车 "Huge" 超大车 "Other" 其他类型
                    //Microbus面包车
                    //SaloonCar小轿车
                    //PassengerCar小红三轮车
                    //MicroTruck小货车
                    //空--未识别
                    //MidTruck中行货车
                    //0- 未知，1- 客车(大型)，2- 货车(大型)，3- 轿车(小型)，4- 面包车，5- 小货车，6- 行人，7- 二轮车，8- 三轮车，9- SUV/MPV，10- 中型客车
                    //string cllx = Encoding.Default.GetString(Result.szVehicleType).Trim('\0');
                    //if (cllx == "SaloonCar")
                    //{
                    //    P44 = "3";
                    //}
                    //else if (cllx == "Microbus")
                    //{
                    //    P44 = "4";
                    //}
                    //else if (cllx == "PassengerCar")
                    //{
                    //    P44 = "8";
                    //}
                    //else if (cllx == "MicroTruck")
                    //{
                    //    P44 = "5";
                    //}
                    //else if (cllx == "MidTruck")
                    //{
                    //    P44 = "2";
                    //}
                    //else
                    //{
                    //    P44 = "0";
                    //}

                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFICJUNCTION)//交通路口事件
                {
                    #region 交通路口事件
                    DEV_EVENT_TRAFFICJUNCTION_INFO Result = (DEV_EVENT_TRAFFICJUNCTION_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;//卡口=0
                    m_count = Result.stuFileInfo.bCount;//卡口=0
                    if (m_index == 0) m_index = 1;
                    if (m_count == 0) m_count = 1;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = m_count.ToString();//Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    cs = Enum_CaptureSystem.卡口;
                    P5 = "0";
                    P6 = "JARI";
                    P7 = "2";
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间

                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    if (Result.stuVehicle.bColor == 1)
                    {
                        P24 = GetColorCode(Result.stuVehicle.rgbaMainColor);//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    }
                    else
                    {
                        P24 = "";
                    }
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    //"Unknown"  未知,"Motor" 机动车,"Non-Motor":非机动车,"Bus": 公交车,"Bicycle" 自行车,"Motorcycle":摩托车
                    //string cllx = Encoding.Default.GetString(Result.stuVehicle.szObjectSubType).Trim('\0');
                    //if ((cllx == "Unknown") || (cllx == "Non-Motor") || (cllx == "Bicycle"||(cllx== "Motorcycle"))) P44 = "small";
                    //if ((cllx == "Bus") || (cllx == "Motor")) P44 = "big";
                    //P44 = cllx;
                    P44 = Encoding.Default.GetString(Result.stuVehicle.szObjectSubType).Trim('\0');
                    P45 = TrimStrangeChar(Encoding.Default.GetString(Result.stuVehicle.szText).Trim(spliter));
                    P46 = bag.PositionString;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_RUNREDLIGHT)//交通违章-闯红灯事件
                {
                    #region 闯红灯
                    DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO Result = (DEV_EVENT_TRAFFIC_RUNREDLIGHT_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.闯红灯;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "4";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_WRONGROUTE)//不按车道行驶
                {
                    #region 不按导向行驶
                    DEV_EVENT_TRAFFIC_WRONGROUTE_INFO Result = (DEV_EVENT_TRAFFIC_WRONGROUTE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.不按导向行驶;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "6";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_RETROGRADE)//逆行
                {
                    #region 逆行
                    DEV_EVENT_TRAFFIC_RETROGRADE_INFO Result = (DEV_EVENT_TRAFFIC_RETROGRADE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.逆行;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "3";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERLINE)//压车道线（压白线）
                {
                    #region 压线
                    DEV_EVENT_TRAFFIC_OVERLINE_INFO Result = (DEV_EVENT_TRAFFIC_OVERLINE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.压线;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "5";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_CROSSLANE)//违章变道
                {
                    //DEV_EVENT_TRAFFIC_CROSSLANE_INFO Info = new DEV_EVENT_TRAFFIC_CROSSLANE_INFO();
                    #region 违章变道
                    DEV_EVENT_TRAFFIC_CROSSLANE_INFO Result = (DEV_EVENT_TRAFFIC_CROSSLANE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.违章变道;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "9";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERSPEED)//交通违章-超速
                {
                    #region 超速
                    DEV_EVENT_TRAFFIC_OVERSPEED_INFO Result = (DEV_EVENT_TRAFFIC_OVERSPEED_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.超速;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "2";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "3";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    if (Result.stuVehicle.bColor == 1)
                    {
                        P24 = GetColorCode(Result.stuVehicle.rgbaMainColor);//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    }
                    else
                    {
                        P24 = "";
                    }
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    P44 = Encoding.Default.GetString(Result.stuVehicle.szObjectSubType).Trim('\0');
                    P45 = TrimStrangeChar(Encoding.Default.GetString(Result.stuVehicle.szText).Trim(spliter));
                    P46 = bag.PositionString;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_TURNLEFT)//交通违章-违章左转
                {
                    //DEV_EVENT_TRAFFIC_TURNLEFT_INFO Result = (DEV_EVENT_TRAFFIC_TURNLEFT_INFO)bag.dhinfo;
                    return;
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_TURNRIGHT)//交通违章-违章右转
                {
                    //DEV_EVENT_TRAFFIC_TURNRIGHT_INFO Info = new DEV_EVENT_TRAFFIC_TURNRIGHT_INFO();
                    return;
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_UNDERSPEED)//交通违章-低速
                {
                    //DEV_EVENT_TRAFFIC_UNDERSPEED_INFO Info = new DEV_EVENT_TRAFFIC_UNDERSPEED_INFO();
                    return;
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_OVERYELLOWLINE)//压黄线
                {
                    #region 压黄线
                    DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO Result = (DEV_EVENT_TRAFFIC_OVERYELLOWLINE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.压线;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "5";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_MANUALSNAP)//交通手动抓拍事件
                {
                    //DEV_EVENT_TRAFFIC_MANUALSNAP_INFO Info = new DEV_EVENT_TRAFFIC_MANUALSNAP_INFO();
                    return;
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKING)//交通违章停车
                {
                    #region 违停
                    DEV_EVENT_TRAFFIC_PARKING_INFO Result = (DEV_EVENT_TRAFFIC_PARKING_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.违章停车;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "12";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = "0";//Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_YELLOWPLATEINLANE)//黄牌占道，闯禁区
                {
                    #region 违停
                    DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO Result = (DEV_EVENT_TRAFFIC_YELLOWPLATEINLANE_INFO)bag.dhinfo;
                    m_index = Result.stuFileInfo.bIndex;
                    m_count = Result.stuFileInfo.bCount;
                    cs = Enum_CaptureSystem.黄牌禁限;
                    P1 = "3.1";
                    P2 = Result.stuFileInfo.nGroupId.ToString();
                    P3 = Result.stuFileInfo.bCount.ToString();
                    P4 = "0";//判断是否是合成图
                    P5 = "11";//记录类型(必须),0-卡口，1-低速，2-超速，3-逆行，4-违反交通灯指示，5-压车道线，6-不按导向，7-路口滞留，8-机占非，9-违法变道，10-机动车违反规定使用专用车道，11-黄牌车禁限，12-监控抓拍
                    P6 = "JARI";
                    P7 = "1";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                    P8 = deviceview.deviceparam.sbbh;
                    P9 = "SJZD" + G.SystemParam.LocalIP;
                    P10 = "13441";//违法代码
                    P11 = deviceview.roadparam.gbdm;
                    P12 = deviceview.roadparam.bmbh;
                    P13 = deviceview.roadparam.xzqh;
                    P14 = deviceview.roadparam.roadid;
                    P15 = deviceview.roadparam.roadname;
                    P16 = ((int)deviceview.deviceparam.direction).ToString();
                    P17 = Result.nLane.ToString();
                    DateTime t = new DateTime((int)Result.UTC.dwYear, (int)Result.UTC.dwMonth, (int)Result.UTC.dwDay, (int)Result.UTC.dwHour, (int)Result.UTC.dwMinute, (int)Result.UTC.dwSecond, (int)Result.UTC.dwMillisecond);
                    P18 = t.ToString("yyyyMMddHHmmssfff");
                    P19 = t.ToString("yyyy-MM-dd HH:mm:ss");
                    P20 = "0";//红灯持续时间(可选),单位：秒，由检测器给出时间
                    P23 = Encoding.Default.GetString(Result.stuObject.szText).Trim('\0');
                    if (Result.stuObject.bColor == 1)
                    {
                        P22 = GetColorString(Result.stuObject.rgbaMainColor);
                        P21 = GetHPZL(P22, P23);
                    }
                    else
                    {
                        P21 = "";
                        P22 = "";
                    }
                    P24 = "";//车身颜色(可选),0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
                    P25 = "";//车辆长度(可选),单位厘米，没有为空字符串
                    P26 = deviceview.deviceparam.speedlimit;//限速(可选),无限速为空字符串
                    P27 = "0";//Result.nSpeed.ToString();
                    P28 = "0";//数据标识(必须),图片数据是否上传，0-不上传1-上传，不上传数据时，图片区不用上传。
                    P29 = G.SystemParam.LocalIP;
                    #endregion
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKINGSPACENOPARKING)//车位无车事件
                {
                    //DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO Info = new DEV_EVENT_TRAFFIC_PARKINGSPACENOPARKING_INFO();
                    return;
                }
                else if (dwAlarmType == EventIvs.EVENT_IVS_TRAFFIC_PARKINGSPACEPARKING)//车位有车事件
                {
                    //DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO Info = new DEV_EVENT_TRAFFIC_PARKINGSPACEPARKING_INFO();
                    return;
                }
                else
                {
                    return;
                }

                #endregion

                #region 2提取图片

                if (m_index == 1)
                {
                    ResetRecord();
                    m_record.Pic1 = bag.pBuffer;
                }
                else if (m_index == 2)
                {
                    m_record.Pic2 = bag.pBuffer;
                }
                else if (m_index == 3)
                {
                    m_record.Pic3 = bag.pBuffer;
                }
                else if (m_index == 4)
                {
                    m_record.Pic4 = bag.pBuffer;
                }
                else if (m_index == 5)
                {
                    m_record.Pic5 = bag.pBuffer;
                }
                else if (m_index == 6)
                {
                    m_record.Pic6 = bag.pBuffer;
                }
                else if (m_index == 7)
                {
                    m_record.Pic7 = bag.pBuffer;
                }
                if (bag.platepic != null)
                {
                    m_record.Pic5 = bag.platepic;
                }
                #endregion

                #region 3信息赋值
                SnapParam sp = (SnapParam)G.SystemParam.SnapParams[cs];
                if (sp.timefrom == m_index)
                {
                    m_record.P18 = P18;
                    m_record.P19 = P19;
                }
                if (sp.platefrom == m_index)
                {
                    m_record.P21 = P21;
                    m_record.P22 = P22;
                    m_record.P23 = P23;
                    m_record.P44 = P44;
                    m_record.P45 = P45;
                    m_record.P46 = P46;
                }
                if (sp.speedfrom == m_index)
                {
                    m_record.P26 = P26;
                    m_record.P27 = P27;
                }
                if (sp.otherfrom == m_index)
                {
                    m_record.P1 = P1;
                    m_record.P2 = P2;
                    m_record.P3 = P3;
                    m_record.P4 = P4;
                    m_record.P5 = P5;
                    m_record.P6 = P6;
                    m_record.P7 = P7;
                    m_record.P8 = P8;
                    m_record.P9 = P9;
                    m_record.P10 = P10;
                    m_record.P11 = P11;
                    m_record.P12 = P12;
                    m_record.P13 = P13;
                    m_record.P14 = P14;
                    m_record.P15 = P15;
                    m_record.P16 = P16;
                    m_record.P17 = P17;
                    m_record.P20 = P20;
                    m_record.P24 = P24;
                    m_record.P25 = P25;
                    m_record.P28 = P28;
                    m_record.P29 = P29;
                }

                #endregion

                #region 3提交处理
                if (LogTool.gDebugEnable) LogTool.log.Info("m_index=" + m_index.ToString() + "m_count=" + m_count.ToString());
                if (m_index == m_count)
                {
                    Worker_Bag workbag = new Worker_Bag();
                    workbag.record = m_record;
                    workbag.view = deviceview;
                    workbag.state = state;
                    //调试用参数
                    workbag.ip = bag.ip;
                    workbag.id = bag.id;
                    workbag.type = bag.type;
                    workbag.bz = bag.bz;
                    if (G.Worker != null) G.Worker.AddJob(workbag);
                }

                #endregion

                GC.Collect();
            }
            catch(Exception er)
            {
                LogTool.log.Info("DHThreadFunc()" + bag.Txt() + er.Message);
            }
        }

        private static string TrimStrangeChar(string str)
        {
            string ans = "";
            try
            {
                if (str != "")
                {
                    int index = 0;
                    bool found = false;
                    for (int i = 0; i < str.Length; i++)
                    {
                        if ((str[i] >= 'A') && (str[i] <= 'z'))
                        {
                            index = i;
                            found = true;
                            break;
                        }
                    }
                    if (found == true)
                    {
                        ans = str.Substring(index);
                    }
                }
            }
            catch { }
            return ans;
        }

        //颜色对照表
        public string GetColorString(uint RGBColor)
        {
            string strColor = RGBColor.ToString();
            uint Color = RGBColor >> 8;
            if (Color == 0x000000)
            {
                strColor = "黑";
            }
            else if (Color == 0xFFFFFF)
            {
                strColor = "白";
            }
            else if (Color == 0xFF0000)
            {
                strColor = "红";
            }
            else if (Color == 0x0000FF)
            {
                strColor = "蓝";
            }
            else if (Color == 0x00FF00)
            {
                strColor = "绿";
            }
            else if (Color == 0xFFFF00)
            {
                strColor = "黄";
            }
            else if (Color == 0x808080)
            {
                strColor = "灰";
            }
            else if (Color == 0xFFA500)
            {
                strColor = "橙";
            }
            else
            {
                strColor = "";
            }

            return strColor;


        }

        //,0-其他色 1-白色 2-银色 3-灰色 4-黑色 5-红色 6-深蓝 7-蓝色 8-黄色 9-绿色 10-棕色 11-粉色 12-紫色，没有为空字符串
        public string GetColorCode(uint RGBColor)
        {
            string strColor = RGBColor.ToString();
            uint Color = RGBColor >> 8;
            if (Color == 0x000000)
            {
                strColor = "4";// "黑";
            }
            else if (Color == 0xFFFFFF)
            {
                strColor = "1";//"白";
            }
            else if (Color == 0xFF0000)
            {
                strColor = "5";// "红";
            }
            else if (Color == 0x0000FF)
            {
                strColor = "7";// "蓝";
            }
            else if (Color == 0x00FF00)
            {
                strColor = "9";// "绿";
            }
            else if (Color == 0xFFFF00)
            {
                strColor = "8";// "黄";
            }
            else if (Color == 0x808080)
            {
                strColor = "3";// "灰";
            }
            else if (Color == 0xFFA500)
            {
                strColor = "11";// "橙";
            }
            else
            {
                strColor = "";
            }

            return strColor;


        }

        public string GetHPZL(string hpys,string hphm)
        {
            string ans = "";
            try
            {
                if (hphm.Contains("警"))
                {
                    ans = "23";
                }
                else if (hphm.Contains("学"))
                {
                    ans = "16";
                }
                else if (hphm.Contains("挂"))
                {
                    ans = "15";
                }
                else if (hpys.Contains("蓝"))
                {
                    ans = "02";
                }
                else if (hpys.Contains("黄"))
                {
                    ans = "01";
                }
                else
                {
                    ans = "";
                }
            }
            catch { ans = ""; }
            return ans;
        }

        public override void AdjustTime(object obj)
        {
            try
            {
                //校时
                DateTime now = DateTime.Now;
                NET_TIME CurTime = new NET_TIME();
                CurTime.dwYear = now.Year;
                CurTime.dwMonth = now.Month;
                CurTime.dwDay = now.Day;
                CurTime.dwHour = now.Hour;
                CurTime.dwMinute = now.Minute;
                CurTime.dwSecond = now.Second;
                bool ans = DHClient.DHSetDevConfig(state.LoginID, CurTime);
                LogTool.log.Info("DH摄像机---" + deviceview.deviceparam.ip + "---" + (ans ? "校时成功" : "校时失败"));
            }
            catch { }
        }

        public override void ShutDown()
        {
            try
            {
                if (m_InitTimer != null)
                {
                    m_InitTimer.Dispose();
                    m_InitTimer = null;
                }

                if (m_TimeAdjuster != null)
                {
                    m_TimeAdjuster.Dispose();
                    m_TimeAdjuster = null;
                }

                if (state.AlarmHandle > 0)
                {
                    DHClient.DHStopLoadPic(state.AlarmHandle);
                    state.AlarmHandle = -1;
                }

                if (state.LoginID > 0)
                {
                    DHClient.DHLogout(state.LoginID);
                    state.LoginID = -1;
                }
            }
            catch { }
        }
    }
}
